package services.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.common.entity.t_flow_switch;
import play.cache.Cache;
import services.base.BaseService;

import common.constants.CacheKey;
import common.interfaces.ICacheable;
import common.utils.Factory;

import daos.common.FlowSwitchDao;

/**
 *@author xiaojun
 *@version 2017-4-27 上午9:39:30
 */
public class FlowSwitchService extends BaseService<t_flow_switch> implements ICacheable{

	private FlowSwitchDao flowSwitchDao;
	protected FlowSwitchService(){
		flowSwitchDao=Factory.getDao(FlowSwitchDao.class);
		 super.basedao=flowSwitchDao;
	}
	/**
	 * 商票背书流转修改开/关
	 * @param flow_switch
	 * @author xiaojun
	 * @version 2017-4-27 上午9:37:02
	 */
	public boolean upFlowSwitch(int flow_switch){
		boolean bln =  flowSwitchDao.upFlowSwitch(flow_switch);
		if (bln){
			clearCache();
		}
		return  bln;
	}
	
	/**
	 * 商票转开修改开/关
	 * @param flow_switch 0 开 1 关
	 * @author xiaojun
	 * @version 2017-4-27 上午9:37:02
	 */
	public boolean upTicketAway(int ticket_away){
		boolean bln =  flowSwitchDao.upTicketAway(ticket_away);
		if (bln){
			clearCache();
		}
		return  bln;
	}
	
	/**
	 * 红包修改开/关
	 * @param flow_switch 0 开 1 关
	 * @author binghui.zhou
	 * @version 2017-4-27 上午9:37:02
	 */
	public boolean upFlowSwitchReward(int reward) {
		boolean bln = flowSwitchDao.upFlowSwitchReward(reward);
		if (bln){
			clearCache();
		}
		return  bln;
	}
	
	/**
	 * CPS修改开/关
	 * @param flow_switch 0 开 1 关
	 * @author binghui.zhou
	 * @version 2017-4-27 上午9:37:02
	 */
	public boolean upTicketAwayCPS(int cps) {
		boolean bln = flowSwitchDao.upTicketAwayCPS(cps);
		if (bln){
			clearCache();
		}
		return  bln;
	}
	
	/**
	 * 平台企业修改开/关
	 * @param flow_switch 0 开 1 关
	 * @author binghui.zhou
	 * @version 2017-4-27 上午9:37:02
	 */
	public boolean updatePlatformCompany(int platform_company) {
		boolean bln=flowSwitchDao.updatePlatformCompany(platform_company);
		if (bln){
			clearCache();
		}
		return bln;	
	}
	
	@Override
	public void addAFlushCache() {
		Map<String,t_flow_switch> flowMap=null; 
		
		List<t_flow_switch> flow=flowSwitchDao.findAll();
		if (flow!=null && flow.size()>0){
			flowMap=new HashMap<String,t_flow_switch>();
			for (t_flow_switch flow_switch:flow){
				flowMap.put(CacheKey.FOREGROUND_CONTROL, flow_switch);
			}
		}
		Cache.safeSet(this.getClass().getName(), flowMap, null);
	}
	
	@Override
	public Map<String,t_flow_switch> getCache() {
		Map<String,t_flow_switch> flowMap=(Map<String, t_flow_switch>) Cache.get(this.getClass().getName());
		if (flowMap == null || flowMap.keySet() == null || flowMap.keySet().size() == 0){
			addAFlushCache();
			
			flowMap=(Map<String, t_flow_switch>) Cache.get(this.getClass().getName());
		}
		return flowMap;
	}
	
	@Override
	public void clearCache() {
		Cache.safeDelete(this.getClass().getName());		
	}
	
	/***
	 * 查找所以的控制器以键值对的形式返回
	 * @return
	 * @author binghui.zhou
	 * @version 2017-4-27
	 */
	public Map<String,t_flow_switch> queryAllSwitchMap(){
		return getCache();
	}
	/**
	 * 领投  修改开/关
	 * @param flow_switch 0 开 1 关
	 * @author liaijun
	 * @version 2017-4-27 上午9:37:02
	 */
	public boolean updateInvest(int collar_switch) {
		 return flowSwitchDao.updateInvest(collar_switch);
	}
	
	/**
	 * 积分开关控制
	 * @param score_switch
	 * @return
	 * @author binghui.zhou
	 * @createDate 2017年8月25日
	 */
	public boolean scroeUpdate(int score_switch) {
		boolean bln=flowSwitchDao.scroeUpdate(score_switch);
		if (bln){
			clearCache();
		}
		return bln;	
	}
	/**
	 *
	 * @param financing
	 * @return
	 * @author xiaojun
	 *@version 2018-3-27 
	 */
	public boolean save(t_flow_switch financing) {
		return flowSwitchDao.save(financing);
	}
}
